package com.nbufe.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.nbufe.pojo.BookInfo;
import com.nbufe.pojo.TypeInfo;
import com.nbufe.service.BookInfoService;
import com.nbufe.service.TypeInfoService;
import com.nbufe.utils.FileNameUtil;
import com.nbufe.vo.ResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Controller
public class BookInfoController {

    //异步上传的图片的名称
    String saveFileName = "";

    @Resource
    private BookInfoService bookInfoService;

    @Resource
    private TypeInfoService typeInfoService;

    /**
     * 图书管理首页
     *
     * @return
     */
    @GetMapping("/bookIndex")
    public String bookIndex() {
        return "book/bookIndex";
    }

    /**
     * 获取book信息，封装成json
     *
     * @param bookInfo
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/bookAll")
    @ResponseBody       //@ResponseBody将java对象转为json格式的数据，表示该方法的返回结果直接写入 HTTP response body 中，一般在异步ajax获取数据时使用
    public ResponseVo bookAll(BookInfo bookInfo, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        PageInfo<BookInfo> pageInfo = bookInfoService.queryBookInfoAll(bookInfo, page, limit);
        return ResponseVo.ok("成功", pageInfo.getTotal(), pageInfo.getList());//总条数getTotal，数据封装成list,以便加载分页显示,由于加了ResponseBody,就会返回一个字符串
    }

    /**
     * 添加页面的跳转
     */
    @GetMapping("/bookAdd")
    public String bookAdd() {
        return "book/bookAdd";
    }

    /**
     * 类型添加提交
     */
    @RequestMapping("/addBookSubmit")
    @ResponseBody
    public ResponseVo addBookSubmit(@RequestBody BookInfo bookInfo) {
        bookInfo.setImgUrl(saveFileName);
        bookInfoService.addBookSubmit(bookInfo);
        saveFileName = "";
        return ResponseVo.ok();
    }

    /**
     * 类型根据id查询(修改)
     */
    @GetMapping("/queryBookInfoById")
    public String queryTypeInfoById(Integer id, Model model) {
        BookInfo bookInfo = bookInfoService.queryBookInfoById(id);
        model.addAttribute("info", bookInfo);
        return "book/updateBook";
    }

    /**
     * 修改提交功能
     */
    @RequestMapping("/updateBookSubmit")
    @ResponseBody
    public ResponseVo updateBookSubmit(@RequestBody BookInfo info) {
        if (!saveFileName.equals("")) {
            info.setImgUrl(saveFileName);
        }
        bookInfoService.updateBookSubmit(info);
        saveFileName = "";
        return ResponseVo.ok();
    }

    /**
     * 类型删除
     */

    @RequestMapping("/deleteBook")
    @ResponseBody
    public ResponseVo deleteBook(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        bookInfoService.deleteBookByIds(list);
        return ResponseVo.ok();
    }

    @RequestMapping("/findAllList")
    @ResponseBody
    public List<TypeInfo> findAll() {
        PageInfo<TypeInfo> pageInfo = typeInfoService.queryTypeInfoAll(null, 1, 100);
        List<TypeInfo> lists = pageInfo.getList();
        return lists;
    }

    @GetMapping("/readerBuyBook")
    public String readerBuyBook() {
        return "book/readerBuyBook";
    }

    //异步ajax文件上传处理
    @ResponseBody
    @RequestMapping("/ajaxImg")
    public Object ajaxImg(MultipartFile imgUrl, HttpServletRequest request) {
        //提取生成文件名UUID+上传图片的后缀.jpg  .png
        saveFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(imgUrl.getOriginalFilename());
//        System.out.println(saveFileName);
        //得到项目中图片存储的路径
        String path = request.getServletContext().getRealPath("/images/book");
//        System.out.println(path);
        //转存  E:\idea_workspace\mimissm\image_big\23sdfasferafdafdadfasfdassf.jpg
        try {
            imgUrl.transferTo(new File(path + File.separator + saveFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回客户端JSON对象,封装图片的路径,为了在页面实现立即回显
        JSONObject object = new JSONObject();
        object.put("imageUrl", saveFileName);
        return object.toString();
    }
}
